#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

#include "net.h"

int initSocket()
{
	int socketId = 0;
	int addrLength = 0;
	struct sockaddr_in addr;
	
	API();
	//创建socket
	socketId = socket(AF_INET, SOCK_STREAM, 0);
	if (socketId < 0)
	{
		perror("socket error");
		return socketId;
	}
	//设置服务器的地址
	addrLength = sizeof(addr);
	memset(&addr, 0, addrLength);
	addr.sin_family = AF_INET;
	addr.sin_port = htons(PORT);
	addr.sin_addr.s_addr = INADDR_ANY;
	
	//绑定自己的地址
	if (0 > bind(socketId, (struct sockaddr *)(&addr), addrLength))
	{
		perror("bind error");
		return -1;
	}
	int on = 1;
	setsockopt(socketId, SOL_SOCKET, SO_REUSEADDR,(void *)on, sizeof(on));
	//监听 
	listen(socketId, 5);
	return socketId;
}

